package com.techfort.metalrss;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.techfort.metalrss.adapters.ItemAdapter;
import com.techfort.metalrss.core.Feed;
import com.techfort.metalrss.core.MetalFeed;
import com.techfort.metalrss.core.MetalFeed.ErrorHandler;
import com.techfort.metalrss.db.FavouriteProvider;
import com.techfort.rssparser.Message;

public class ListFeeds extends Activity {
	private Builder builder;
	private AlertDialog dialog;
	private MetalHandler mcl_handler = new MetalHandler();
	public static final String TAG = "INFO";
	private FavouriteProvider fp;
	private Button add;
	private TextView title;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listfeeds);
        
        fp = new FavouriteProvider(this);
        fp.open();
        
        Bundle extras = this.getIntent().getExtras();
        final Feed feed = extras.getParcelable("FEED");

        add = (Button) findViewById(R.id.add_favourite);
        title = (TextView) findViewById(R.id.title);
        title.setText(feed.getFeed_name());
        
        try {
	        final Feed check = fp.select(feed);
	        
	        if(check!=null){
	        	if(check.getId()==feed.getId()){
	        		add.setText("Remove from favourites");
	        		
	        		add.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
							fp.delete(check);
							add.setEnabled(false);
						}
					});
	        		
	        	}
	        }
        } catch(Exception e) {
        		
    		add.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					fp.insert(feed);
					add.setEnabled(false);
				}
			});
        		
        }
        
        
        
        
        
        ErrorHandler errorHandler = new ErrorHandler() {
			
			@Override
			public void handleError() {
				builder = new AlertDialog.Builder(ListFeeds.this);
				builder.setPositiveButton("ok", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mcl_handler.sendEmptyMessage(0x1337);	
					}
				});
				builder.setMessage("Error occured while fetching feed. Try later");
				dialog = builder.create();
				dialog.show();
				
			}
		};
        try {
        	//List<Message> messages = new MetalFeed(rssurl).getMessages();
        	List<Message> messages = new MetalFeed(feed.getLink()).getMessages();
        	ListView list = (ListView) findViewById(R.id.mainlist);
        
        	list.setAdapter(new ItemAdapter(this, R.layout.rssitem, messages));
        } catch(Exception e){
        	
        	errorHandler.handleError();
        	
        }
    }
    @Override
	public void onStop() {
		super.onStop();
		if(fp!=null){
			fp.close();
		}
	}
	
   
    
    private class MetalHandler extends Handler {
    	public void handleMessage(android.os.Message msg){
    		if(dialog!=null){
    			dialog.dismiss();
    		}
    	}
    }
    
    public FavouriteProvider getProvider(){
    	return this.fp;
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	MenuInflater mi = new MenuInflater(this);
    	mi.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()){
    	case R.id.favourites:
    		Intent intent = new Intent(ListFeeds.this, Favourites.class);
    		startActivity(intent);
    	case R.id.home:
    		Intent home = new Intent(ListFeeds.this, Main.class);
    		startActivity(home);
    	default:
    		return true;
    	}
    }
    
 
}
